//
//  JzVc.swift
//  GjCup
//
//  Created by abc on 2024/4/9.
//

import UIKit

class JzVc: UIViewController {
    
    
    @IBOutlet weak var jzItem: UIBarButtonItem!
    
    @IBOutlet weak var numTf: UITextField!
    
    
    @IBOutlet weak var bSBt: UIButton!{
        didSet{
            bSBt.layer.cornerRadius = 8
        }
    }
    
    @IBOutlet weak var erLa: UILabel!
    
    @IBOutlet weak var baLa: UILabel!
    
    @IBOutlet weak var shiLa: UILabel!
    
    @IBOutlet weak var shiLiuLa: UILabel!
    var type = 0
    
    @IBOutlet weak var bgView: UIView!{
        didSet{
            bgView.layer.cornerRadius = 12
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    
    @IBAction func beginAc(_ sender: UIButton) {
        
        let text = numTf.text ?? ""
        if text.isEmpty {
            UIAlertController.ok_alert("请输入要转换的数字", self)
            return
        }

        var ten = ""
        
        if type == 0 {
            // 二进制
            ten = erToTen(text: text)
        }else if type == 1{
            // 八进制
            ten = baToTen(text: text)
        }else if type == 2{
            // 十进制
            ten = text
        }else if type == 3{
            // 十六进制
            ten = shiliuToTen(text: text)
        }
        
        erLa.text  = "二进制:" + ZZBConvertBinary.zzb_decimal(ten)
        baLa.text = "八进制:" + ZZBConvertOctal.zzb_decimal(ten)
        shiLa.text = "十进制:" + ten
        shiLiuLa.text = "十六进制:" + ZZBConvertHex.zzb_decimal(ten)

    }
    
    
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
        if segue.identifier == "JzToC" {
            let vc = segue.destination as! JzCVc
            let titles = ["二进制","八进制","十进制","十六进制"]

            vc.block = {[weak self](index) in
                self?.type = index
                self?.jzItem.title = titles[index]
            }
        }
    }
    

}
extension JzVc{
    
    // 二进制转十进制
    func erToTen(text: String) -> String {
        
        return ZZBConvertDecimal.zzb_binary(text)
    }
    // 八进制转十进制
    func baToTen(text: String) -> String {
        return ZZBConvertDecimal.zzb_octal(text)
    }
    // 十六进制转十进制
    func shiliuToTen(text: String) -> String {
        return ZZBConvertDecimal.zzb_hex(text)
    }
    
}
